import REST from "@module/Rest";

import "./style.scss";
class ShopHeader extends React.Component {
    constructor() {
        super();
        this.state = {
            info: null,
            isInteresting: false
        };
        // this.customerClist = this.customerClist.bind(this);
    }
    componentDidMount() {
        this.supplierShopInfo();
        this.isInterestSupplier();
    }

    isInterestSupplier() {
        let url = `/store/api/user/buyer/isInterestSupplier`;
        const { id } = this.props;
        REST.post(url, { supplierId: id }).then(
            data => {
                this.setState({ isInteresting: data.isInteresting });
            },
            error => {}
        );
    }

    supplierShopInfo() {
        // let url = `/store/api/user/supplier/getSupplierInfo`;
        let url = global.hasLogin
            ? `/store/api/user/supplier/getSupplierInfo`
            : `/store/api/unlogin/supplier/getSupplierInfo`;
        const { id } = this.props;
        let params = { id };
        REST.post(url, params).then(
            data => {
                this.setState({
                    info: data.supplier
                });
            },
            error => {}
        );
    }

    interestSupplier = () => {
        let url = `/store/api/user/buyer/updateInterestSupplier`;
        const { id } = this.props;

        let params = {
            supplierId: id,
            isInteresting: !this.state.isInteresting
        };
        REST.post(url, params).then(
            data => {
                this.setState({ isInteresting: params.isInteresting });
            },
            error => {}
        );
    };

    render() {
        const { info, isInteresting } = this.state;
        return (
            <div className="rj_shop_header">
                {info && (
                    <div className="rj_shop-wp">
                        <img className="rj_shop-logo" src={info.photo} alt="" />
                        <div className="rj_customer-shop">
                            <p
                                dangerouslySetInnerHTML={{ __html: info.name }}
                            />
                            {global.hasLogin && (
                                <span
                                    className="rj_actice-shop"
                                    onClick={this.interestSupplier}
                                >
                                    {isInteresting ? "取消关注" : "关注商家"}
                                </span>
                            )}
                        </div>
                    </div>
                )}
            </div>
        );
    }
}

export default ShopHeader;
